<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>
<body>
  <div id="root">
    <p :style="{opacity}">Vue So Easy</p>
    <button @click="$destroy()">不活了</button>
  </div>
  <script>
    new Vue({
      el: '#root',
      data: {
        opacity: 1
      },

      // 初始化挂载真实DOM后执行: 启动初始化异步任务(发ajax请求/启动定时器)
      mounted () {
        this.intervalId = setInterval(() => {
          console.log('-----')
          this.opacity -= 0.1
          if (this.opacity<=0) {
            this.opacity = 1
          }
        }, 200);
      },

      // 在死亡之前执行: 做一些收尾的工作(清除定时器)
      beforeDestroy () {
        clearInterval(this.intervalId)
      }
    })
  </script>
</body>
</html>